package com.egao.ap.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.ap.biz.InformationZanBiz;
import com.egao.ap.entity.InformationZan;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-10-23 23:27:18
 * @version ：1.0
 */
@Repository
public class InformationZanBizImpl extends BaseDao implements InformationZanBiz {

	@Override
	public JsonResult list(PageResult2<T> page, InformationZan informationZan, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<InformationZan> rm = new BeanPropertyRowMapper<InformationZan>(InformationZan.class);
		StringBuffer sql = new StringBuffer("select i.id,i.organization_id,i.openid");
        sql.append(",i.infoid,i.create_time,i.create_user,i.update_time");
		sql.append(",i.update_user");
        sql.append(",suser.username createName ");
        sql.append(" from a_information_zan i ");
        sql.append(" inner join sys_user suser on suser.user_id=i.create_user ");
        sql.append(" where i.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and i.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(informationZan)){
//			if(Utils.isNotEmpty(informationZan.getName())){
//				sql.append(" and instr(i.name,:name)>0 ");
//				map.put("name", informationZan.getName());
//			}
			if(Utils.isNotEmpty(informationZan.getSort())&&Utils.isNotEmpty(informationZan.getOrder())){
                sql.append(" order by i."+informationZan.getSort()+" "+informationZan.getOrder());
            } else {
                sql.append(" order by i.create_time desc");
            }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<InformationZan> list(InformationZan informationZan, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<InformationZan> rm = new BeanPropertyRowMapper<InformationZan>(InformationZan.class);
        StringBuffer sql = new StringBuffer("select i.id,i.organization_id,i.openid");
        sql.append(",i.infoid,i.create_time,i.create_user,i.update_time");
		sql.append(",i.update_user");
        sql.append(",suser.username createName ");
        sql.append(" from a_information_zan i ");
        sql.append(" inner join sys_user suser on suser.user_id=i.create_user ");
        sql.append(" where i.deleted=0  ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and i.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(informationZan)){
//			if(Utils.isNotEmpty(informationZan.getName())){
//				sql.append(" and instr(i.name,:name)>0 ");
//				map.put("name", informationZan.getName());
//			}
            if(Utils.isNotEmpty(informationZan.getSort())&&Utils.isNotEmpty(informationZan.getOrder())){
				sql.append(" order by i."+informationZan.getSort()+" "+informationZan.getOrder());
			} else {
				sql.append(" order by i.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public InformationZan getById(InformationZan informationZan) {
		RowMapper<InformationZan> rm = new BeanPropertyRowMapper<InformationZan>(InformationZan.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select i.id,i.organization_id,i.openid");
        sql.append(",i.infoid,i.create_time,i.create_user,i.update_time");
		sql.append(",i.update_user");
        sql.append(",suser.username createName ");
        sql.append(" from a_information_zan i ");
        sql.append(" inner join sys_user suser on suser.user_id=i.create_user ");
        sql.append(" where i.deleted=0 and i.id=:id ");
		map.put("id", informationZan.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public Integer save(InformationZan informationZan, User loginUser) {
		GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
		KeyHolder keyHolder=new GeneratedKeyHolder();
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into a_information_zan (");
        sql.append("openid,infoid,create_user,create_time) values ( ");
        sql.append(":openid,:infoid,:create_user,now())");
        map.put("openid", loginUser.getXcxopenid());
        map.put("infoid", informationZan.getInfoid()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int retVal = this.jdbcTemplate.update(sql.toString(), new SqlParameterSource() {
			@Override
			public boolean hasValue(String paramName) {
				return map.containsKey(paramName);
			}
			@Override
			public Object getValue(String paramName) throws IllegalArgumentException {
				return map.get(paramName);
			}
		}, generatedKeyHolder);

		Integer id = generatedKeyHolder.getKey().intValue();
		return id;
	}

	@Override
	public boolean update(InformationZan informationZan, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update a_information_zan set ");
        sql.append("openid=:openid,infoid=:infoid,update_time=now() ");
		sql.append(",update_user=:update_user where id=:id ");
        map.put("openid", informationZan.getOpenid());
        map.put("infoid", informationZan.getInfoid()==null?null:informationZan.getInfoid()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", informationZan.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(InformationZan informationZan, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(informationZan.getId().length()>0){
			for(String ID : informationZan.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("delete from  a_information_zan "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
